package service.impl;

import dao.ICartDao;
import dao.impl.CartDaoImpl;
import pojo.Cart;
import service.ICartService;

import java.util.List;

public class CartServiceImpl implements ICartService {
    private final ICartDao cartDao = new CartDaoImpl();

    /**
     * 添加到购物车
     *
     * @param cart
     * @return
     */
    @Override
    public boolean insertToCart(Cart cart) {
        Cart c = cartDao.queryByUidAndGid(cart.getUid(), cart.getGid());
        if (c != null) {
            return cartDao.updateCountByUidAndGid(cart.getG_count(), cart.getUid(), cart.getGid());
        }
        return cartDao.insertToCart(cart);
    }

    /**
     * 根据uid查询购物车数据
     *
     * @param uid
     * @return List<Cart>
     */
    @Override
    public List<Cart> queryByUid(int uid) {
        return cartDao.queryByUid(uid);
    }

    /**
     * 根据uid清空购物车
     *
     * @param uid
     * @return 改变行数
     */
    @Override
    public int clearCartByUid(int uid) {
        return cartDao.clearCartByUid(uid);
    }

    /**
     * 根据gid从购物车中删除数据
     *
     * @param gid
     * @return 改变行数
     */
    @Override
    public boolean removeFromCartByUidAndGid(int uid, int gid) {
        return cartDao.removeFromCartByUidAndGid(uid,gid) == 1;
    }
}
